package com.peng.sell.service.impl;

import com.peng.sell.config.WechatAccountConfig;
import com.peng.sell.dto.OrderDTO;
import com.peng.sell.service.SendMessageService;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.WxMpTemplateMsgService;
import me.chanjar.weixin.mp.api.impl.WxMpTemplateMsgServiceImpl;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
@Service
@Slf4j
public class SendMessageServiceImpl implements SendMessageService {

    @Autowired
    private WxMpService wxMpService;

    @Autowired
    private WechatAccountConfig accountConfig;
    @Override
    public void orderStatus(OrderDTO orderDTO) {
        WxMpTemplateMessage templateMessage = new WxMpTemplateMessage();
        templateMessage.setTemplateId(accountConfig.getTemplateId().get("orderStatus"));
        templateMessage.setToUser(orderDTO.getBuyerOpenid());
        templateMessage.setUrl("http://www.baidu.com");
        List<WxMpTemplateData> templateDataList = Arrays.asList(
                new WxMpTemplateData("desc","亲，请记得收获哦"),
                new WxMpTemplateData("sellerName","微信点餐"),
                new WxMpTemplateData("sellerPhone","15603261393"),
                new WxMpTemplateData("orderNo",orderDTO.getOrderId()),
                new WxMpTemplateData("status",orderDTO.getOrderStatusEnum().getMsg()),
                new WxMpTemplateData("totalPrice","￥"+orderDTO.getOrderAmount()),
                new WxMpTemplateData("remark","欢迎再来~")
        );
        templateMessage.setData(templateDataList);
        try {
            wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage);
        } catch (WxErrorException e) {
            log.error("[订单消息推送]出现异常：{}",e);
        }

    }
}
